<?php

class DefaultController extends AdminController
{
	// public function accessRules()
	// {
	// 	return array(
 //            array('allow',  // allow all users to perform  actions
 //            	// 'actions'=>array('', '', '', '', ''),
 //            	// 'users'=>array('*'),
 //            	),  
 //            array('allow',   //allow authenticated user to perform actions
 //            	'actions'=>array('index'),
 //            	'users'=>array('@'),
 //            	),
 //            array('deny',  // deny all users
 //            	'users'=>array('*'),
 //            	),            
 //            );
	// }
	public function actionIndex()
	{
		$user = Yii::app()->user->getState('user');
		if($user['id'] === null){
			$this->actionLogin();
		}
		else{
			$this->render('index');
		} 
			
	}
	public function actionLogin()
	{

		$model=new AdminLoginForm;

		if(isset($_POST['AdminLoginForm']))
		{
			// print_r($_POST['AdminLoginForm']);die();
			$result = User::model()->findByAttributes(array('username' => $_POST['AdminLoginForm']['username']));
			// print_r($result);die();
			if(isset($result)){
				$identity=new UserIdentity($_POST['AdminLoginForm']['username'],$_POST['AdminLoginForm']['password']);
				// print_r($identity->authenticate());exit();
				switch ($identity->authenticate()) {
					// print_r($identity->authenticate());exit();
					case 2:
						$msg = "Sai mật khẩu";
						$this->renderPartial('login',array('msg' => $msg));
						// Yii::app()->end();
						break;

					default:							
						Yii::app()->user->login($identity,3600*24*7);
						$this->actionIndex();
						break;
				}
			}
			else{
				$msg = "Sai tên đăng nhập";
				$this->renderPartial('login', array('msg'=>$msg));
			}
		}
		else{
			$this->renderPartial('login', array('model'=>$model));
		}
		// $this->layout = null;
		
	}

	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->createAbsoluteUrl('xachdoadmin/default/login/'));
	}
}